# 李广龙
# 服务器sh脚本
# 利用docker-compose启动
# down 卸载
# up 启动
# up -d 后台启动
# 7.14.2 支持arm版本


echo 版本:7.1.0


SERVICE_PATH="/data/es"

creatConfig() {
# kibana 配置文件
mkdir -p $SERVICE_PATH"/kibana/config"
# es ik分词器存放地址,解压后放入该文件夹
mkdir -p $SERVICE_PATH"/plugins"
# 数据挂载目录
mkdir -p $SERVICE_PATH"/elasticsearch/data"
mkdir -p $SERVICE_PATH"/elasticsearch/plugins"

cd $SERVICE_PATH"/kibana/config" || exit
cat >./kibana.yml <<EOF
#
# ** THIS IS AN AUTO-GENERATED FILE **
# 李广龙-2022.05.18
#
# Default Kibana configuration for docker target


server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"

EOF
}
creatDockerCompose() {
cat >./docker-compose.yml <<EOF
version: '3'
services:
  elasticsearch-single:
    image: elasticsearch:7.1.0
    container_name: elasticsearch-single
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
      - 9300:9300
    volumes:
      - $SERVICE_PATH/elasticsearch/plugins:/usr/share/elasticsearch/plugins
    networks:
      - esnet
  kibana:
    image: kibana:7.1.0
    container_name: kibana
    environment:
      - ELASTICSEARCH_URL=http://elasticsearch-single:9200
      - ELASTICSEARCH_HOSTS=http://elasticsearch-single:9200
    ports:
      - "5601:5601"
    volumes:
      - $SERVICE_PATH/kibana/config:/usr/share/kibana/config
    networks:
      - esnet
networks:
  esnet:

EOF
}
creatStart() {
  creatConfig
  start
}

cpik() {
  docker cp $SERVICE_PATH/plugins/ik/ elasticsearch-single:/usr/share/elasticsearch/plugins
  docker restart elasticsearch-single
}

read -p '输入功能编号 0:创建配置目录 1:创建docker-compose文件: (任意键退出)' input
echo "输入编号:$input"

case $input in
    0)
    creatConfig
    # 创建完成，上传ik分词器到 plugins
    ;;
    1)
    creatDockerCompose
    ;;
    2)
    #cpik
    # 本地ik分词器安装到docker内
    ;;
    *)
    echo -e "${RED}退出sh脚本${RES}"
    exit 0
    ;;
  esac

# 1、先上传脚本
# 2、创建配置目录
# 3、上传ik解压后的到/elasticsearch/plugins 目录下
# 4、生成docker-compose脚本
# 5、启动 docker-compose up -d


# es默认用户名：elastic
# 如果需要单独设置密码





